package cdu.javaendwork.util.pageHelp;


import cdu.javaendwork.dao.Universal;
import cdu.javaendwork.dao.impl.UniversalImpl;


public class PageHelper {

    Universal universal = new UniversalImpl();



    public PageInfo all(String objName, int pageIndex , int pageSize)
    {

        //处理pageInfo
        PageInfo pageInfo = new PageInfo();
        int sumCount = universal.findSumCount(objName);


        //将总条目数处理成总页数
        int t = sumCount;
        sumCount = sumCount / pageSize;
        if(t % pageSize != 0) sumCount ++;

        //检查pageIndex的合法性
        if(pageIndex < 1) pageIndex = 1;
        if(pageIndex > sumCount) pageIndex = sumCount;

        //处理是否有下一页上一页
        boolean isNext = false;
        if(t > pageIndex * pageSize) isNext = true;
        boolean isLast = false;
        if(pageIndex > 1) isLast = true;

        //封装pageInfo
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        pageInfo.setSumCount(sumCount);
        pageInfo.setIsNext(isNext);
        pageInfo.setIsLast(isLast);

        //测试输出
        System.out.println(pageInfo);

        return pageInfo;
    }
    public PageInfo filter(String objName, String where , int pageIndex , int pageSize)
    {

        //处理pageInfo
        PageInfo pageInfo = new PageInfo();
        int sumCount = universal.filterFindSumCount(objName , where);


        //将总条目数处理成总页数
        int t = sumCount;
        sumCount = sumCount / pageSize;
        if(t % pageSize != 0) sumCount ++;

        //检查pageIndex的合法性
        if(pageIndex < 1) pageIndex = 1;
        if(pageIndex > sumCount) pageIndex = sumCount;

        //处理是否有下一页上一页
        boolean isNext = false;
        if(t > pageIndex * pageSize) isNext = true;
        boolean isLast = false;
        if(pageIndex > 1) isLast = true;

        //封装pageInfo
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        pageInfo.setSumCount(sumCount);
        pageInfo.setIsNext(isNext);
        pageInfo.setIsLast(isLast);

        //测试输出
        System.out.println(pageInfo);

        return pageInfo;
    }
}
